﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AMS.Core.Services;
using AMS.Core.Cryptography;
using AMS.Core.Data;

namespace AMS.DD35.WebMVP.Models
{
    public class RegisterModel : IRegisterModel
    {
        IRegisterService _register;

        #region Register Observer
        public event EventHandler RegistrationFailed;
        protected void RaiseRegisterFailed(EventArgs e)
        {
            if (RegistrationFailed != null)
                RegistrationFailed(this, e);
        }

        public event EventHandler RegistrationSucceeded;
        protected void RaiseRegisterSucceeded(EventArgs args)
        {
            if (RegistrationSucceeded != null)
                RegistrationSucceeded(this, args);
        }
        #endregion

        public RegisterModel(IRegisterService registerService)
        {
            this._register = registerService;
        }

        public void Register(string userName, string firstName, string lastName, string password, string email)
        {
            Hasher hash = new Hasher();
            string pwHash = hash.GetHash(password);
            // TODO: implement server-stored salt

            try
            {
                _register.AttemptRegister(new RegistrationData(userName, pwHash, firstName, lastName, email));
                RaiseRegisterSucceeded(EventArgs.Empty);
            }
            catch (Exception)
            {
                RaiseRegisterFailed(EventArgs.Empty);
            }
        }
    }
}